package com.common.service.judicialpersonal.impl;

import com.common.converter.JobChangeExperienceConverter;
import com.common.entity.personal.JobChangeExperienceEntity;
import com.common.global.BizException;
import com.common.global.CommonEnum;
import com.common.repository.judicialpersonal.JobChangeExperienceRepository;
import com.common.requestvo.judicialpersonal.JobChangeExperienceReqVo;
import com.common.service.judicialpersonal.JobChangeExperienceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * JobChangeExperience 检察官岗位变更经历 ServiceImpl
 *
 * @author johnny
 * @create 2021-05-14 6:03 下午
 **/
@Slf4j
@Service
@Transactional(rollbackOn = Exception.class)
public class JobChangeExperienceServiceImpl implements JobChangeExperienceService {

    private final JobChangeExperienceRepository jobChangeExperienceRepository;

    public JobChangeExperienceServiceImpl(JobChangeExperienceRepository jobChangeExperienceRepository) {
        this.jobChangeExperienceRepository = jobChangeExperienceRepository;
    }

    @Override
    public void add(JobChangeExperienceReqVo jobChangeExperienceReqVo) {
        jobChangeExperienceReqVo.setId(null);
        JobChangeExperienceEntity jobChangeExperienceEntity = JobChangeExperienceConverter.INSTANCE.vo2domain(jobChangeExperienceReqVo);
        jobChangeExperienceRepository.save(jobChangeExperienceEntity);
    }

    @Override
    public void edit(JobChangeExperienceReqVo jobChangeExperienceReqVo) {
        if (jobChangeExperienceReqVo.getId() != null && jobChangeExperienceReqVo.getId() != 0L) {
            if (jobChangeExperienceRepository.existsById(jobChangeExperienceReqVo.getId())) {
                JobChangeExperienceEntity jobChangeExperienceEntity = JobChangeExperienceConverter.INSTANCE.vo2domain(jobChangeExperienceReqVo);
                jobChangeExperienceRepository.save(jobChangeExperienceEntity);
            } else {
                throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
            }
        } else {
            throw new BizException(CommonEnum.ID_NOT_NULL);
        }
    }

    @Override
    public void delete(Long id) {
        if (jobChangeExperienceRepository.existsById(id)) {
            jobChangeExperienceRepository.deleteById(id);
        } else {
            throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
        }

    }
}